package com.ssms.service;

import com.ssms.commons.Result;
import com.ssms.commons.TablePage;
import com.ssms.dao.ClazzMapper;
import com.ssms.dao.ExamMapper;
import com.ssms.dao.SubjectMapper;
import com.ssms.model.Clazz;
import com.ssms.model.Exam;
import com.ssms.model.Exam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ExamService {

    @Resource
    private ExamMapper examMapper;
    @Resource
    private ClazzMapper clazzMapper;

    public Result save(Exam exam,Integer instor) {
        Result check = check(exam,instor);
        if(check != null){
            return check;
        }
        int count = examMapper.insertSelective(exam);
        return count > 0?Result.success():Result.failed("添加失败", 500);
    }


    public TablePage search(TablePage tablePage, Exam exam) {
        int size = tablePage.getLimit();
        int start = (tablePage.getPage() - 1) * size;
        // 获取分页数据
        List<Exam> roles = examMapper.selectPage(start, size, exam);
        // 获取符合条件的总数
        int count = examMapper.selectPageCount(exam);
        tablePage.setCount(count);
        tablePage.setData(roles);
        return tablePage;
    }

    public Result update(Exam exam,Integer instor) {
        Result check = check(exam,instor);
        if(check != null){
            return check;
        }
        int count = examMapper.updateByPrimaryKeySelective(exam);
        return count > 0?Result.success():Result.failed("更新失败", 500);
    }

    // 校验考试冲突
    private Result check(Exam exam,Integer instor){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Exam param = new Exam();
        param.setId(exam.getId());

        param.setAddress(exam.getAddress());
        param.setExamDate(exam.getExamDate());
        param.setStartTime(exam.getStartTime());
        param.setEndTime(exam.getEndTime());
        int i = examMapper.checkExamConflict(param); // 校验地点和考试时间
        if(i > 0){
            return Result.failed("考场【"+exam.getAddress()+"】在" + sf.format(exam.getExamDate())
                    +" " + exam.getStartTime() + "-" + exam.getEndTime() + "时段已被占用",500);
        }

        param.setAddress(null);
        param.setClazzId(exam.getClazzId());
        int j = examMapper.checkExamConflict(param); // 校验学生和考试时间
        if(j > 0){
            Clazz clazz = clazzMapper.selectByPrimaryKey(exam.getClazzId());
            return Result.failed("班级【"+clazz.getClazzName()+"】在" + sf.format(exam.getExamDate())
                    +" " + exam.getStartTime() + "-" + exam.getEndTime() + "时段已存在考试",500);
        }

        param.setCourseId(exam.getCourseId());
        int k = examMapper.checkCourseConflict(param);
        if(k > 0){
            Clazz clazz = clazzMapper.selectByPrimaryKey(exam.getClazzId());
            return Result.failed("班级【"+clazz.getClazzName()+"】已存在该科目的考试安排",500);
        }


        if(instor != null){
            List<Integer> ownerClazzIds = getOwnerClazzIds(instor);
            if(!ownerClazzIds.contains(exam.getClazzId())){
                return Result.failed("只能发布自己所带班级考试信息",500);
            }
        }

        return null;
    }

    // 获取辅导员自身所带班级id
    private List<Integer> getOwnerClazzIds(Integer instor){
        List<Integer> list = new ArrayList<>();
        List<Clazz> clazzes = clazzMapper.selectByInstructor(instor);
        if(clazzes != null){
            for (Clazz clazz : clazzes) {
                list.add(clazz.getId());
            }
        }
        return list;
    }

    public Result delete(Integer id) {
        int count = examMapper.deleteByPrimaryKey(id);
        return count > 0?Result.success():Result.failed("删除失败", 500);
    }
}
